using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using iTron.Model.GridItems;

namespace iTron.Model.Player
{
    class StupidAI : AI
    {
        private int action;
        private Bike bike;
        private int bikeX;
        private int bikeY;

        public StupidAI()
        {

        }

        public bool checkDirection(int dir, GridItem[,] grid)
        {
            switch (dir)
            {
                case Utils.DIRECTION_TOP:
                    if (bikeY - 1 <= 0)
                    {
                        return false;
                    }
                    if (grid[bikeY - 1, bikeX].isTrail
                        || grid[bikeY - 1, bikeX].isBike)
                    {
                        return false;
                    }
                    break;
                case Utils.DIRECTION_RIGHT:
                    if (bikeX >= Grid.GRID_WIDTH - 1)
                    {
                        return false;
                    }
                    if (grid[bikeX + 1, bikeX].isTrail
                        || grid[bikeX + 1, bikeX].isBike)
                    {
                        return false;
                    }
                    break;
                case Utils.DIRECTION_BOTTOM:
                    if (bikeY >= Grid.GRID_HEIGHT - 1) {
                        return false;
                    }
                    if (grid[bikeY + 1, bikeX].isTrail
                            || grid[bikeY + 1, bikeX].isBike)
                    {
                        return false;
                    }
                    break;
                case Utils.DIRECTION_LEFT:
                    if (bikeX - 1 <= 0)
                    {
                        return false;
                    }
                    if (grid[bikeX - 1, bikeX].isTrail
                        || grid[bikeX - 1, bikeX].isBike)
                    {
                        return false;
                    }
                    break;
            }

            return true;
        }

        public override int getAction(GridItem[,] grid)
        {
            getBike(grid);

            int dir = bike.directionTo;

            // Check current
            if (checkDirection(dir, grid))
            {
                return 0;
            }

            // Check left
            dir = (dir <= 1) ? Utils.DIRECTION_LEFT : dir--;
            if (checkDirection(dir, grid))
            {
                return Utils.BUTTON_LEFT;
            }

            // Choose right
            dir += 2;
            if (dir == 6) dir = 2;
            return Utils.BUTTON_RIGHT;
        }

        public void getBike(GridItem[,] grid)
        {
            for (int i = 0; i < Grid.GRID_HEIGHT; i++)
            {
                for (int j = 0; j < Grid.GRID_WIDTH; j++)
                {
                    if (grid[i, j].isBike && grid[i, j].playerID == this.ID)
                    {
                        this.bike = (Bike) grid[i, j];
                        bikeY = i;
                        bikeX = j;
                    }
                }
            }
        }
    }
}
